package com.bolt.report.engine.model;


import com.bolt.common.collection.IteratorUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Row {

    private int height;

    private int realHeight = -1;

    private List<Cell> cells;

    public Row(int columnSize) {
        this.cells = new ArrayList<>(columnSize);
        for (int i = 0; i < columnSize; i++) {
            cells.add(null);
        }
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public List<Cell> getCells() {
        return cells;
    }

    public void setCells(List<Cell> cells) {
        this.cells = cells;
    }

    public void setCell(Column column, Cell cell) {
        if (cells == null) {
            cells = new ArrayList<>();
        }

        int colIdx = column.index;
        if (colIdx >= cells.size()) {
            for (int i = cells.size(); i <= colIdx; ++i) {
                cells.add(null);
            }
        }
        cells.set(colIdx, cell);
    }

    public void addCell(Cell cell) {
        if (cells == null) {
            cells = new ArrayList<>();
        }
        cells.add(cell);
    }

    public void setCell(Cell cell) {
        if (cells != null) {
            cells.set(cell.getColumnIndex(), cell);
        }
    }

    public Cell getCell(Column column) {
        return getCell(column.index);
    }


    public Cell getCell(int index) {
        if (cells == null || cells.size() <= index) {
            return null;
        }
        return cells.get(index);
    }

    public boolean isEmpty() {
        boolean isEmpty = true;
        if (IteratorUtil.isEmpty(cells)) {
            return isEmpty;
        } else {
            Iterator it = cells.iterator();
            while (it.hasNext()) {
                if (null != it.next()) {
                    isEmpty = false;
                    break;
                }
            }
        }
        return isEmpty;
    }

    public void removeCell(Cell cell) {
        this.cells.set(cell.getColumnIndex(), null);
    }

    public int getRealHeight() {
        if (realHeight == -1) {
            return height;
        }
        return realHeight;
    }




    public void setRealHeight(int realHeight) {
        this.realHeight = realHeight;
    }
}